package com.goldcn.user.service;

import com.goldcn.enums.ImcsEnum;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.ImcsConfigMapper;
import com.goldcn.user.model.ImcsConfig;
import com.goldcn.util.CommonTools;
import com.google.common.base.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/9/13.
 */
@Service
@Transactional
public class ImcsConfigService {
    @Autowired
    ImcsConfigMapper imcsConfigMapper;
    @Autowired
    RestTemplate restTemplate;

    public ImcsConfig selectBySupplierID(Long supplierID){
        if(Objects.equal(supplierID,0L)){
            return getPlatConfig();
        }
        ImcsConfig imcsConfig = imcsConfigMapper.selectBySupplierID(supplierID);
        if(imcsConfig == null){
            return getPlatConfig();
        }
        return imcsConfig;
    }


    private ImcsConfig getPlatConfig(){
        String tenantId = getExt(ImcsEnum.TENANTID.getCode());
        String imServiceId = getExt(ImcsEnum.IMSERVICEID.getCode());
        String projectId = getExt(ImcsEnum.PROJECTID.getCode());
        String plugUrl = getExt(ImcsEnum.PLOGURL.getCode());
        ImcsConfig imcsConfig = new ImcsConfig();
        imcsConfig.setConfigID(0L);
        imcsConfig.setSupplierID(0L);
        imcsConfig.setTenantId(tenantId);
        imcsConfig.setImServiceId(imServiceId);
        imcsConfig.setProjectId(projectId);
        imcsConfig.setPlugUrl(plugUrl);
        imcsConfig.setEmail("");
        imcsConfig.setRemark("");
        imcsConfig.setCompanyID(SystemContext.getCompanyID());
        return imcsConfig;
    }

    private String getExt(String code){
        return CommonTools.getExtString(restTemplate,code);
    }

    public int insertSelective(ImcsConfig imcsConfig){
        return imcsConfigMapper.insertSelective(imcsConfig);
    }

}
